<?php
namespace Admin\Controller;

class IndexController extends BaseController {

	public $Version;
	public $System;
	public $SAy_c;
	public $Article;
	public $Link;
	public $Album;
	public $Gust;
	public $Say;
	public $picture;
	public $Album_c;
	public $Article_c;


	public function __construct()
	{
		parent::__construct();
		$this->Version   = D('version');
		$this->System    = D('system');
		$this->SAy_c	 = D('say_c');
    	$this->Article   = D('article');
		$this->Link      = D('link');
		$this->Album     = D('album');
		$this->Gust      = D('gust');
		$this->Say 	     = D('say');
		$this->picture   = D('picture');
		$this->Album_c   = D('album_c');
		$this->Article_c = D('article_c');

	}


    public function index(){
        // 缓存初始化
        //S(array('type'=>'xcache','expire'=>60));

        //---------------统计数据-----------
        //$res = $this->Version->where('1')
        //->fetchSql(true)
        //->select();
        //var_dump($res);

        if(!$version = S('aversion')){
        	$version = $this->Version->order('v_id desc')->limit(1)->getField('v_version');
        	//setS("aversion", $version);
        }
        $this->version = $version;

        if(!$system = S('asystem')){
        	$system = $this->System->find();
        	//setS('asystem',$system);
        }

		if(!$num = S('anum')){
	      	$say_n = $this->SAy_c->count();
			$article_n = $this->Article_c->count();
			$album_n = $this->Album_c->count();
	      	$num = array(
				'say'     	=> $this->Say->count(),
	            'article'  	=> $this->Article->count(),
	            'comment'   => $say_n+$article_n+$album_n,
	            'gust'	  	=> $this->Gust->count(),
				'album'		=> $this->Album->count(),
				'picture'	=> $this->picture->count(),
				'link'		=> $this->Link->count(),
			);
			//setS("anum",$num);
		}
		$this->assign("num",$num);

		//----------------最新评论------------------------------+
		if(!$s_content = S('as_content')){
	        $time = $this->Article_c->field('ac_time')
	        		->table('__ARTICLE_C__')
	            	->union(array('SELECT sc_time FROM th_say_c ','SELECT alc_time FROM th_album_c order by ac_time desc limit 0,5'),ture)
	            	//->fetchSql(true)
	                ->select();

		    for($i=0;$i<5;$i++){
	            $a  = $this->Article_c->where(array('ac_time'=>$time[$i]['ac_time']))->find();
	            $al = $this->Album_c->where(array('alc_time'=>$time[$i]['ac_time']))->find();
	            $s  = $this->SAy_c->where(array('sc_time'=>$time[$i]['ac_time']))->find();
				if($a!=''){
					$s_content[] = $a;
				}elseif($al!=''){
					$s_content[] = $al;
				}elseif($s!=''){
					$s_content[] = $s;
				}
	        }
	        //setS('as_content',$s_content);
		}
		$this->assign('s_content',$s_content);

		if(!$gusts = S('agusts')){
			$gusts = $this->Gust->where('g_rtime>0')->order('g_time desc')->limit(5)->select();
			//setS("agusts", $gusts);
		}
		$this->gusts = $gusts;
    	$this->assign("index","class='active'");
        //var_dump($s_content);
        //exit();

        $this->display();

    }
    /*    退出     */
    public function out(){
		session(null);
		$this->redirect("Admin/login");
	}

}